package com.http;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

public class HttpCl {

	private HttpContext _HTTP_CONTEXT;
	private HttpClient _httpclient;
	
	public HttpCl()
	{
		//init http client
		_HTTP_CONTEXT = new BasicHttpContext();
        _HTTP_CONTEXT.setAttribute(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/8.0.1");
        _httpclient = new DefaultHttpClient();
	}
	
	public InputStream execRequest(String url)
	{
		HttpGet httpget = new HttpGet(url);
		HttpResponse response = null;
		InputStream requestResult = null;
		
		try {
			response = _httpclient.execute(httpget, _HTTP_CONTEXT);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpEntity entity = response.getEntity();
		
		try {
			requestResult = entity.getContent();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return requestResult;
	}
}
